﻿using System;

namespace Microsoft.Goldenlight.Collections
{
    /// <summary>
    /// 	Represents a space partition structure based on QuadTree.
    /// </summary>
    /// <typeparam name="T"> Specifies the element type of the tree. </typeparam>
    public class QuadTree<T>
    {
        #region Fields & Properties

        private object _syncRoot;
        private int _count;

        private QuadTreeNode<T> _root;

        /// <summary>
        /// 	Gets the root node of the QuadTree.
        /// </summary>
        /// <returns> The root QuadTreeNode of the QuadTree. </returns>
        public QuadTreeNode<T> Root
        {
            get
            {
                return _root;
            }
        }

        private int _maxDepth;

        /// <summary>
        /// </summary>
        public int MaxDepth
        {
            get
            {
                return _maxDepth;
            }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// 	Initializes a new instance of the QuadTree class.
        /// </summary>
        /// <param name="maxDepth"> The max depth of the QuadTree. </param>
        public QuadTree(int maxDepth) : this(QuadTreeNode<T>.Allocate(), maxDepth)
        {
        }

        /// <summary>
        /// 	Initializes a new instance of the QuadTree class.
        /// </summary>
        /// <param name="root"> The root QuadTreeNode. </param>
        /// <param name="maxDepth"> The max depth of the QuadTree. </param>
        public QuadTree(QuadTreeNode<T> root, int maxDepth)
        {
            if (root == null)
            {
                throw new ArgumentNullException("root");
            }
            if (maxDepth < 0)
            {
                throw new ArgumentOutOfRangeException("maxDepth");
            }
            _root = root;
            _root.Tree = this;
            _root.Parent = null;
            _root.Depth = 0;
            _maxDepth = maxDepth;
        }

        #endregion
    }
}